class Shop::SpuParametersController < ShopController
  before_action :set_spu

  def index
    @spu_parameters = @spu.spu_parameters.order("created_at ASC")
  end

  def new
    @spu_parameter = SpuParameter.new
  end

  def create
    @spu_parameter = @spu.spu_parameters.build spu_parameter_params
    if @spu_parameter.save
      redirect_to shop_spu_spu_parameters_path(@spu), notice: "保存成功！"
    else
      redirect_to shop_spu_spu_parameters_path(@spu), notice: "保存失败！"
    end
  end

  def edit
    @spu_parameter = SpuParameter.find params[:id]
  end
  
  def update
    @spu_parameter = SpuParameter.find params[:id]
    if @spu_parameter.update spu_parameter_params
      redirect_to shop_spu_spu_parameters_path(@spu)
    else
      render :edit
    end
  end
  
  def destroy
    @spu_parameter = SpuParameter.find params[:id]
    if @spu_parameter.destroy
      redirect_to shop_spu_spu_parameters_path(@spu), notice: "删除成功."
    else
      redirect_to shop_spu_spu_parameters_path(@spu), notice: "删除失败."
    end
  end

  private

  def set_spu
    @spu = Spu.find params[:spu_id]
  end

  def spu_parameter_params
    params.require(:spu_parameter).permit(:param_key, :param_value, :spu_id)
  end
end
